#include <REGX52.H>

unsigned char Re_KeyNum ;

int Key(void) //独立按键，按下按键，返回对应数值1、2、3、4,无按键按下则返回0
{
	if(P3_1 == 0) { return 1;}
	if(P3_0 == 0) { return 2;}
	if(P3_2 == 0) { return 3;}
	if(P3_3 == 0) { return 4;}
	
	return 0;
}

void Key_Loop(void)
{
	static unsigned char LastNum, NowNum;

	LastNum = NowNum;//弹起时，LastNum为0，NowNum = 1~4，这样就可以扫描到按键的状态了
	NowNum  = Key();

	if(LastNum == 1 && NowNum == 0) { Re_KeyNum = 1;}
	if(LastNum == 2 && NowNum == 0) { Re_KeyNum = 2;}
	if(LastNum == 3 && NowNum == 0) { Re_KeyNum = 3;}
	if(LastNum == 4 && NowNum == 0) { Re_KeyNum = 4;}
	
}

unsigned char Re_Value(void)//返回按键的值
{
	unsigned char Temp;
	
	Temp = Re_KeyNum;
	
	Re_KeyNum = 0;
	
	return Temp;
	
}